//
//  DABaseViewController.m
//  DAKnowledge
//
//  Created by DreamArts on 16/4/26.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DABaseViewController.h"
#import "DANetWorkReachabilityStatus.h"
#import "DAloginPersonalInformation.h"

@interface DABaseViewController ()

@property (nonatomic, copy) NSString *status;
@property (nonatomic, strong) DAloginPersonalInformation *personalInformation;
@property (nonatomic, copy) NSString *userPath;
@end

@implementation DABaseViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    /** 调用设置导航栏颜色 */
    [self setNavigationColor];
    self.view.backgroundColor = [UIColor colorWithHexString:backGroundColor];
    [self prepareUserID];
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
         [self showNetWorkStatus];
      //  self.userDefaultKeys =[NSMutableArray array];
    });
    self.requestTimeOut = NSLocalizedString(@"requestServerTimeOut", nil);
    self.netWorkPortConnection = NSLocalizedString(@"netWorkPortConnection", nil);
    self.netIsNotAvailabel = NSLocalizedString(@"theCurrentNetWorkIsNotAvailabel", nil);
    self.requestFailed = NSLocalizedString(@"requestDataFailed", nil);
}
#pragma mark - 取出userID
- (void)prepareUserID{
    /** 解档*/
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).lastObject stringByAppendingString:@"DALogin"];
    self.personalInformation = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    self.userID = self.personalInformation.userID;
}

// 从本地取出个人信息的对象
- (NSString *)readLocalTheUserInfoData{
    DAloginPersonalInformation *personal = [[DAloginPersonalInformation alloc]init];
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).lastObject stringByAppendingString:@"DALogin"];
    personal = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSString *userName = personal.userName;
    return userName;
}
#pragma mark - 设置导航栏的背景颜色
- (void)setNavigationColor{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.barTintColor = [UIColor supportGreenColor];
    self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    NSString *popBack = NSLocalizedString(@"popBack", nil);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:popBack style:UIBarButtonItemStylePlain target:self action:@selector(disMissViewController)];
    self.navigationItem.backBarButtonItem = backItem;
}
- (void)disMissViewController{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 计算文本高度
- (CGFloat)heightWithText:(NSString *)text Width:(CGFloat)width Font:(CGFloat)font{
    if (text == nil) {
        return 0;
    }
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
    CGSize size = CGSizeMake(width, 20000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    /** 返回得到的高度 */
    return rect.size.height;
}
#pragma mark - 网络状态判断
- (void)showNetWorkStatus{
    [DANetWorkReachabilityStatus netWorkSuccessBlock:^(NSString *status) {
        NSLog(@"网络状态 %@", status);
        if ([status isEqualToString:@"当前网络不可用"]){
            //[self showAlert:status];
           // [self loadMBProgress:status];
        }else if ([status isEqualToString:@"正在使用手机流量"]){
           // [self showAlert:status];
           // [self loadMBProgress:status];
        } else if ([status isEqualToString:@"wifi状态下"]){
            //[self showAlert:status];
            //[self loadMBProgress:status];
        }
       // [self finishLoadDataMBProgress];
        
        self.status = status;
    }];
}
#pragma mark - showAlert
- (void)showAlert:(NSString *)str{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleAlert];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window.rootViewController presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}
#pragma mark - 加载菊花
- (void)loadMBProgress:(NSString *)str{
    [SVProgressHUD showInfoWithStatus:str];
//    self.hud = [[MBProgressHUD alloc]initWithView:self.view];
//    self.hud.dimBackground = NO;
//    /** 设置加载文字*/
//    self.hud.labelText = str;
//    self.hud.minShowTime = 1;
//    self.hud.activityIndicatorColor = [UIColor themeWhiteColor];
//    self.hud.labelColor = [UIColor themeWhiteColor];
//    self.hud.mode = MBProgressHUDModeText;
//    [[UIApplication sharedApplication].keyWindow addSubview:self.hud];
//   // [self.view addSubview:self.hud];
//    [self.hud showAnimated:NO whileExecutingBlock:^{
//    } completionBlock:^{
//        [self.hud removeFromSuperview];
//        self.hud = nil;
//    }];
}

- (void)loadDataMBProgress{
    [SVProgressHUD show];
//    self.hudMB = [[MBProgressHUD alloc]initWithView:self.view];
//    self.hudMB.dimBackground = YES;
//    self.hudMB.minShowTime = 5;
//    self.hudMB.labelColor = [UIColor themeWhiteColor];
//    [[UIApplication sharedApplication].keyWindow addSubview:self.hudMB];
//    // [self.view addSubview:self.hud];
//    [self.hudMB showAnimated:NO whileExecutingBlock:^{
//    } completionBlock:^{
//        [self.hudMB removeFromSuperview];
//        self.hudMB = nil;
//    }];
}

- (void)finishLoadDataMBProgress{
    [SVProgressHUD dismiss];
}

//- (void)isSaveDataWithUrl:(NSString *)url dic:(NSDictionary *)dic{
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    NSString *path = [NSString stringWithFormat:@"%ld%@.plist", [url hash], dic];
//    BOOL isSave = [userDefaults boolForKey:path];
//    if (isSave) {
//        NSLog(@"存储过数据");
//    }else{
//        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
//        [SVProgressHUD showWithStatus:@"正在加载数据"];
//    }
//}
//
//- (BOOL)isSavedDataWithUrl:(NSString *)url dic:(NSDictionary *)dic{
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    NSString *path;
//    if (dic != nil) {
//        path = [NSString stringWithFormat:@"%ld%@.plist",[url hash], dic[@"page"]];
//    }else{
//        path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
//    }
//    BOOL isSaved = [userDefaults boolForKey:path];
//    self.userDefaultKeys = [DABaseViewController shareInstance];
//    
//    
//    for (NSString *userPath  in self.userDefaultKeys) {
//        if (![userPath isEqualToString:path]) {
//            [self.userDefaultKeys addObject:path];
//        }
//    }
//    NSLog(@"path : %@", path);
//    NSLog(@"userDefaultKeys: %@", self.userDefaultKeys);
//    return isSaved;
//}
//
//#pragma mark - 根据路径 获取存在本地的数据
//- (NSDictionary *)readTheLocalDataWith:(NSString *)url dic:(NSDictionary *)dic{
//    NSString *path = [NSString stringWithFormat:@"%ld%@.plist", [url hash], dic];
//     NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
//    id result  = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
//    NSData *data = result;
//    NSError *Error = nil;
//    NSDictionary *resultDic;
//    if (data != nil) {
//        resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&Error];
//    }
//    return resultDic;
//}
//
//
//+ (NSMutableArray *)shareInstance{
//    
//    static NSMutableArray *array = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        array = [NSMutableArray array];
//    });
//    
//    return array;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
   
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
